package com.joymart.user.rest;


import com.joymart.common.error.handling.JoymartBusinessException;
import com.joymart.common.utils.SecurityUtils;
import com.joymart.user.service.UserHealthReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Optional;

@RestController
public class UserHealthReportController {

    @Autowired
    private UserHealthReportService userHealthReportService;




    @GetMapping("/healthReport")
    public Object userHealthReport() {
        return userHealthReportService.findByUserId(SecurityUtils.getCurrentUserId());
    }

    @PutMapping("/healthReport")
    public void updateHealthReport(@RequestBody Map<String, Object> body) {
        String url = Optional.ofNullable(body.get("url"))
                .map(String.class::cast).orElseThrow(() -> new JoymartBusinessException("缺少url参数"));
        userHealthReportService.upsertUserHealthReport(SecurityUtils.getCurrentUserId(), url);
    }

    @GetMapping("/admin/healthReport/{userId}")
    public Object userHealthReport(@PathVariable String userId) {
        return userHealthReportService.findByUserId(userId);
    }
}
